package com.mach.platform.sprider.dev.zz;


import com.mach.platform.sprider.Conf;
import com.mach.platform.sprider.dev.ByteUtil;
import com.mach.platform.sprider.dev.DataHelper;
import com.mach.platform.util.SqlHelper;
import org.joda.time.DateTime;

import java.io.IOException;

/*
    取得变频器数据抓取器,一个对象针对一个采集模块:电表
 */
public class CongYaDBDev extends DataHelper {

    protected String devName;

    protected SqlHelper sqls;

    public static void main(String[] args) throws IOException {
        //scan();
//        sendSth();
        findAll();
    }
    public static void findAll() throws IOException {
        int busNumStartIx =11,busNumEndIx =11;// 总线地址开始与结束参数
        int tcpPortStartIx = 10001,tcpPortEndIx = 10001; //TCP端口开始与结束参数
        String ip = "192.168.4.61";
        for(int j = tcpPortStartIx; j <= tcpPortEndIx; j+=1){
            for(int i = busNumStartIx; i <= busNumEndIx; i+=1){
                //0x015E

                for(int k =9; k <=9; ++k){
                    readIdById(ip,j,i,"XXXX",k, 3);
                }
            }
        }
    }

    static void sendSth(){

        CongYaDBDev db  = new CongYaDBDev("192.168.4.21", 10002, 1, "");
        Integer value = db.getEnery();

        System.out.println("value="+value);
    }

    private void test() {
        Integer result = null;

        DataHelper dh = this;

        if(dh == null)return;

        byte[] repData = new byte[0];
        try {
            repData = dh.getDataRtu(0x0106,4,3);
            if(repData.length <1)return;
            byte[] data = ByteUtil.subData(repData,3,2);
            //data = ByteUtil.reverse2Byte(data);
            data = ByteUtil.reverse(data);
            result = new Integer(ByteUtil.getShort(data));
            System.out.println("result=" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    static void scan(){
        int ports = 4;
        int devs = 4;
        for(int i = 0; i<ports; ++i) {
            for(int ii = 0; ii<devs; ++ii) {
                CongYaDBDev db  = new CongYaDBDev("192.168.1.200", 8001+i, 1+ii, "");

                Integer value = db.getElect();
                //Integer value2 = db.getVolt();

                System.out.println("ports=" + (i + 1) + " dev=" + (ii+ 1) +"  value="+ value + " value2="+ 0);
            }
        }

    }

    public CongYaDBDev(String ip, Integer port, Integer devID, String devName) {
        super(ip,port,devID);

        this.sqls  = Conf.getSqlHelper();

        this.devName = devName;
    }

    @Override
    public void run(){

        long preSec = 0;
        long nowSec = 0;
        while(true){


            try {

//                long nowMill = System.currentTimeMillis();
//
//                nowSec = nowMill / 1000;
//
//                if(nowSec == preSec)continue;
//
//                Date now = new Date(nowMill);
//
//                int fyear = now.getYear() + 1900;
//                int fmonth = now.getMonth() + 1;
//                int fday = now.getDate();
//                int fhour = now.getHours();
//                int fmin = now.getMinutes();
//                int fsec = now.getSeconds();

                DateTime dateTime = new DateTime();
                int fyear = dateTime.getYear();
                int fmonth = dateTime.getMonthOfYear();
                int fday = dateTime.getDayOfMonth();
                int fhour = dateTime.getHourOfDay();
                int fmin = dateTime.getMinuteOfHour();
                int fsec = dateTime.getSecondOfMinute();

                Object volt =  this.getVolt();
                Object elect = this.getElect();
                Object power = this.getPower();
                Object enery = this.getEnery();

                try {
                    this.sqls.updateAsync("insert ignore into pv_data_detail(lv1,lv2,lv3,dev_id, fyear, fmonth, fday, fhour, fmin, fsec,fdatetime, " +
                            "volt,elect,power,enery) value" +
                            "('ZZ','ALL','ALL','"+this.devName+"',"+fyear+","+fmonth+","+fday+","+fhour+","+fmin+","+fsec+","+"date_format(concat("+fyear+",'-',"+fmonth+",'-',"+fday+",' ',"+fhour+",':',"+fmin+",':',"+fsec+")," +
                            "'%Y-%m-%d %H:%i:%s'),"
                            +volt+","+elect+","+power+","+enery+")");
                } catch (Exception e) {
                    e.printStackTrace();
                }

                try {
                    this.sqls.updateAsync("replace into data_cur (dev_id,volt,elect,power,update_time) value " +
                            "( " +
                            "'"+this.devName+"',"+
                            volt + ","+
                            elect + ","+
                            power + "," +
                            "date_format(concat("+fyear+",'-',"+fmonth+",'-',"+fday+",' ',"+fhour+",':',"+fmin+",':',"+fsec+")," +
                                    "'%Y-%m-%d %H:%i:%s')" +
                            ")");
                } catch (Exception e) {
                    e.printStackTrace();
                }
//                Thread.sleep(1000*60);


            } catch (Exception e) {
                e.printStackTrace();
            }

            preSec = nowSec;


        }


    }

    /*
      取变频器:电压
   */
    public Integer getVolt() {
        Integer result = null;

        DataHelper dh = this;

        if(dh == null)return null;

        byte[] repData = new byte[0];
        try {
            repData = dh.getDataRtu(0x0134,4,3);
            if(repData.length <1)return null;
            byte[] data = ByteUtil.subData(repData,3,2);
            //data = ByteUtil.reverse2Byte(data);
            data = ByteUtil.reverse(data);
            result = new Integer(ByteUtil.getShort(data)  /10);
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println("excep devId:" + this.devName + " " + e.getMessage());
        }


        return result;
    }

    /*
     取变频器:电流
  */
    public Integer getElect() {
        Integer result = null;

        DataHelper dh = this;

        if(dh == null)return null;

        byte[] repData = new byte[0];
        try {
            repData = dh.getDataRtu(0x013C,4,3);
            if(repData.length <1)return null;
            byte[] data = ByteUtil.subData(repData,3,2);
            //data = ByteUtil.reverse2Byte(data);
            data = ByteUtil.reverse(data);
            result = new Integer(ByteUtil.getShort(data)  * 300 / 1000);
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println("excep devId:" + this.devName + " " + e.getMessage());
        }


        return result;
    }

    /*
     取变频器:功率
  */
    public Integer getPower() {
        Integer result = null;

        DataHelper dh = this;

        if(dh == null)return null;

        byte[] repData = new byte[0];
        try {
            repData = dh.getDataRtu(0x0141,4,3);
            if(repData.length <1)return null;
            byte[] data = ByteUtil.subData(repData,3,2);
//            data = ByteUtil.reverse2Byte(data);
            data = ByteUtil.reverse(data);
            result = new Integer(ByteUtil.getShort(data) );
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println("excep devId:" + this.devName + " " + e.getMessage());
        }


        return result;
    }

    /*
    取变频器:能量
 */
    public Integer getEnery() {
        Integer result = null;

        DataHelper dh = this;

        if(dh == null)return null;

        byte[] repData = new byte[0];
        try {
            repData = dh.getDataRtu(0x015E,8,3);
            if(repData.length <1)return null;
            byte[] data = ByteUtil.subData(repData,3,4);
            data = ByteUtil.reverse2Byte(data);
            data = ByteUtil.reverse(data);
            result = ByteUtil.getInt(data);
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println("excep devId:" + this.devName + " " + e.getMessage());
        }


        return result;
    }

    public static void readIdById(String ip, Integer port, Integer id, String devName, Integer addr, Integer cmdCode) throws IOException {
        CongYaDBDev dev =  new CongYaDBDev(ip,port,id,devName);
        byte[] data = dev.readSth(addr, 2, cmdCode);
//        U.printData(data);
    }

}
